<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form
                class="mb-[-16px]"
                :model="queryParams"
                inline
            >
                <el-form-item label="房间标题" prop="name">
                    <el-input class="w-[280px]" v-model="queryParams.name" clearable placeholder="请输入房间标题" />
                </el-form-item>
                <el-form-item label="房间类型" prop="type">
                    <el-checkbox-group v-model="checkedTypes" :max="1" @change="handleTypeChange">
                        <el-checkbox :label="1">山也客房</el-checkbox>
                        <el-checkbox :label="2">宠物有好房</el-checkbox>
                    </el-checkbox-group>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button v-perms="['room/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <!-- <el-button
                v-perms="['room/delete']"
                :disabled="!selectData.length"
                @click="handleDelete(selectData, 'delete')"
            >
                删除
            </el-button> -->
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <!-- <el-table-column type="selection" width="55" /> -->
                    <el-table-column label="房间标题" prop="name" show-overflow-tooltip />
                    <el-table-column label="房价" prop="price" show-overflow-tooltip />
                    <el-table-column label="房间数量" prop="number" show-overflow-tooltip />
                    <!-- <el-table-column label="是否有效 0否 1是" prop="status" show-overflow-tooltip /> -->
                    <el-table-column label="封面图" prop="cover" show-overflow-tooltip >
                        <template #default="{ row }">
                            <el-image :src="row.cover" style="width: 100px; height: 100px;" :fit="row.cover.includes('http') ? 'cover' : 'contain'"/>
                        </template>
                    </el-table-column>
                    <el-table-column label="房间类型" prop="type" show-overflow-tooltip>
                        <template #default="{ row }">
                            {{ row.type == 1 ? '山也客房' : '宠物有好房' }}
                        </template>
                    </el-table-column>
                    <!-- 轮播图 -->
                    <el-table-column label="轮播图" prop="photos" show-overflow-tooltip >
                        <template #default="{ row }">
                            <el-image :src="jsonToArr(row.photos)[0]" style="width: 100px; height: 100px;" :fit="row.photos.includes('http') ? 'cover' : 'contain'" :preview-src-list="jsonToArr(row.photos)" :preview-teleported="true"/>
                        </template>
                    </el-table-column>
                    <el-table-column label="基础配置信息" prop="config" show-overflow-tooltip >
                        <template #default="{ row }">
                            <div style="width: 100%;display: flex;flex-direction: column;align-items: flex-start;">
                                <div v-for="(item, index) in jsonToArr(row.config, 'config')" :key="index" style="display: flex;align-items: center;">
                                    <el-image :src="item.icon" style="width: 20px; height: 20px;" />
                                    <span>
                                        {{ item.text }}
                                    </span>
                                </div>
                            </div>
                            
                        </template>
                    </el-table-column>
                    <el-table-column label="服务项" prop="basic_config" show-overflow-tooltip >
                        <template #default="{ row }">
                            <!-- 根据接口返回的服务项数据id，例如 '11,2,13,5',渲染对应id 的name -->
                            <div style="width: 100%;display: flex;flex-direction: column;align-items: flex-start;">
                                <div v-for="(item, index) in jsonToArr(row.basic_config)" :key="index">
                                    {{ hotelBasicList.find((item2: any) => item2.id == item)?.title }}
                                </div>
                            </div>
                            
                        </template>
                    </el-table-column>
                    <el-table-column label="状态" prop="status_txt" show-overflow-tooltip />
                    <el-table-column label="操作" width="180" fixed="right">
                        <template #default="{ row }">
                             <el-button
                                v-perms="['room/edit']"
                                type="primary"
                                link
                                @click="handleEdit(row)"
                            >
                                编辑
                            </el-button>
                            <el-button
                                type="primary"
                                link
                            >
                            <router-link
                                :to="{
                                    path: '/hotelManage/datePrice',
                                    query: {
                                        id: row.id
                                    }
                                }"
                            >配置</router-link>
                                
                            </el-button>
                            <el-button
                                v-perms="['room/delete']"
                                type="danger"
                                v-if="row.status_txt == '正常'"
                                link
                                @click="handleDelete(row.id, 'delete')"
                            >
                                删除
                            </el-button>
                            <el-button
                                v-perms="['room/delete']"
                                v-if="row.status_txt == '删除'"
                                type="danger"
                                link
                                @click="handleDelete(row.id, 'restore')"
                            >
                                恢复
                            </el-button>
                            
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="roomLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiRoomLists, apiRoomDelete, apiRoomRestore } from '@/api/room'
import { timeFormat } from '@/utils/util'
import { apiHotelBasicAll } from '@/api/hotel'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import { useRouter } from 'vue-router'
import { getRoutePath } from '@/router'

const router = useRouter()
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)

// 选中的房间类型
const checkedTypes = ref<number[]>([])

// 查询条件
const queryParams = reactive({
    name: '',
    type: '' as any,
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
const { pager, getLists, resetPage } = usePaging({
    fetchFun: apiRoomLists,
    params: queryParams
})
const resetParams = () => {
    queryParams.name = ''
    queryParams.type = ''
    checkedTypes.value = []
    getLists()
}

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[], type: string) => {
    if (type == 'delete') {
        await feedback.confirm('确定要删除？')
        await apiRoomDelete({ id })
        getLists()
    } else {
        await feedback.confirm('确定要恢复？')
        await apiRoomRestore({ id })
        getLists()
    }
}

// 房间类型变化
const handleTypeChange = () => {
    queryParams.type = checkedTypes.value[0]
}

getLists()

// 配置
const handleConfig = (row: any) => {
    router.push({
        path: '/room/date_price',
        query: { id: row.id }
    })
}

// JSON字符串转数组
const jsonToArr = (jsonStr: string, key?: string) => {
    try {
        if(key == 'config' || key == 'other_config'){
            return JSON.parse(jsonStr)
        }else{
            return jsonStr.split(',')
        }
        // return JSON.parse(jsonStr)
    } catch (error) {
        return []
    }
}
const hotelBasicList = ref<any[]>([])
const getApiHotelBasicAllFun = async () => {
    const res = await apiHotelBasicAll({})
    hotelBasicList.value = res
}
onMounted(() => {
    getApiHotelBasicAllFun()
})
</script>

